package com.fdb.efp.nls.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.RscWarnLoanDao;
import com.fdb.efp.nls.service.domain.RscWarnLoan;
import com.fdb.efp.nls.service.facade.RscWarnLoanService;
import com.fdb.efp.nls.service.vo.RscWarnLoanVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>ServiceImpl: 风险预警台账				<p>
 * @author chenqm1
 * @date 2018-05-07
 */
@Service("rscWarnLoanService")
public class RscWarnLoanServiceImpl implements RscWarnLoanService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(RscWarnLoanServiceImpl.class);

	@Autowired
	private RscWarnLoanDao rscWarnLoanDao;

	/**
	 * 新增操作
	 */
	public int insertRscWarnLoan(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("当前新增数据为:"+ inRscWarnLoanVo.toString());
		int num = 0;
		try {
			RscWarnLoan rscWarnLoan = new RscWarnLoan();
			beanCopy(inRscWarnLoanVo, rscWarnLoan);
			num = rscWarnLoanDao.insertRscWarnLoan(rscWarnLoan);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("当前删除数据条件为:"+ inRscWarnLoanVo);
		int num = 0;
		try {
			RscWarnLoan rscWarnLoan = new RscWarnLoan();
			beanCopy(inRscWarnLoanVo, rscWarnLoan);
			num = rscWarnLoanDao.deleteByPk(rscWarnLoan);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscWarnLoanVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("当前修改数据为:"+ inRscWarnLoanVo.toString());
		int num = 0;
		try {
			RscWarnLoan rscWarnLoan = new RscWarnLoan();
			beanCopy(inRscWarnLoanVo, rscWarnLoan);
			num = rscWarnLoanDao.updateByPk(rscWarnLoan);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscWarnLoanVo +"修改的数据条数为"+ num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public RscWarnLoanVO queryByPk(RscWarnLoanVO inRscWarnLoanVo) {

		logger.debug("当前查询参数信息为:"+ inRscWarnLoanVo);
		try {
			RscWarnLoan queryRscWarnLoan = new RscWarnLoan();
			beanCopy(inRscWarnLoanVo, queryRscWarnLoan);
			RscWarnLoan queryRslRscWarnLoan = rscWarnLoanDao.queryByPk(queryRscWarnLoan);
			if (Objects.nonNull(queryRslRscWarnLoan)) {
				RscWarnLoanVO outRscWarnLoanVo = beanCopy(queryRslRscWarnLoan, new RscWarnLoanVO());
				logger.debug("当前查询结果为:"+ outRscWarnLoanVo.toString());
				return outRscWarnLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<RscWarnLoanVO> queryAllOwner(RscWarnLoanVO rscWarnLoanVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryAllOwnerByPage(rscWarnLoanVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ rscWarnLoans.size());
			pageSet(rscWarnLoans, rscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<RscWarnLoanVO> queryAllCurrOrg(RscWarnLoanVO rscWarnLoanVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryAllCurrOrgByPage(rscWarnLoanVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+rscWarnLoans.size());
		List<RscWarnLoanVO> list = null;
		try {
			pageSet(rscWarnLoans, rscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<RscWarnLoanVO> queryAllCurrDownOrg(RscWarnLoanVO rscWarnLoanVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryAllCurrDownOrgByPage(rscWarnLoanVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ rscWarnLoans.size());
		List<RscWarnLoanVO> list = null;
		try {
			pageSet(rscWarnLoans, rscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RscWarnLoanVO> queryRscWarnLoanOut(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("当前查询参数信息为:"+ inRscWarnLoanVo);
		String taskNo=inRscWarnLoanVo.getTaskNo();
		String[] taskNoStrs = taskNo.split(",");
		List<String> taskNos = Arrays.asList(taskNoStrs);

		String ruleCode=inRscWarnLoanVo.getRuleCode();
		String[] ruleCodeStrs = ruleCode.split(",");
		List<String> ruleCodes = Arrays.asList(ruleCodeStrs);

		RscWarnLoan rscWarnLoan = new RscWarnLoan();
		beanCopy(inRscWarnLoanVo, rscWarnLoan);
		List<RscWarnLoanVO> queryRscWarnLoan11 = rscWarnLoanDao.queryRscWarnLoan(rscWarnLoan);
		List<RscWarnLoanVO> list = new ArrayList<RscWarnLoanVO>(taskNos.size());
		List<RscWarnLoanVO> Outlist =null;
		try {
			for (int i = 0; i < taskNos.size(); i++) {
				RscWarnLoanVO rscWarnLoanVO = new RscWarnLoanVO();
				rscWarnLoanVO.setTaskNo(taskNos.get(i));
				rscWarnLoanVO.setRuleCode(ruleCodes.get(i));
				list.add(rscWarnLoanVO);
			}

			List<RscWarnLoanVO> result = null;
			for (int i = 0;i < list.size(); i++) {
				List<RscWarnLoanVO> queryRscWarnLoan = rscWarnLoanDao.queryRscWarnLoanOut(list.get(i));
				result = (List<RscWarnLoanVO>) beansCopy(queryRscWarnLoan, RscWarnLoanVO.class);

				if(Outlist == null) {
					Outlist = Collections.EMPTY_LIST;
				}
				Outlist.addAll(result);
			}

		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return Outlist;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RscWarnLoanVO> queryByContNo() {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryByContNo();
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ rscWarnLoans.size());
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RscWarnLoanVO> queryBytaskNo(RscWarnLoanVO rscWarnLoanVO) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryBytaskNo(rscWarnLoanVO);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ rscWarnLoans.size());
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RscWarnLoanVO> queryRscWarnLoanByContNos(RscWarnLoanVO inRscWarnLoanVo, List<String> contNos) {
		if ( null == contNos || contNos.size() == 0) return null;

		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryRscWarnLoanByContNosByPage(contNos);
			logger.debug("根据客户正将号码列表查询风险预警台账的结果集数量为:"+ rscWarnLoans.size());
            pageSet(rscWarnLoans, inRscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public int updateRscData(RscWarnLoanVO rscWarnLoanVO) {
		logger.debug("当前修改数据为:"+ rscWarnLoanVO.toString());
		int num = 0;
		try {
			RscWarnLoan rscWarnLoan = new RscWarnLoan();
			beanCopy(rscWarnLoanVO, rscWarnLoan);
			num = rscWarnLoanDao.updateRscData(rscWarnLoan);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ rscWarnLoanVO +"修改的数据条数为"+ num);
		return num;
	}

	@Override
	public RscWarnLoanVO queryByCond(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("当前查询条件为:"+ inRscWarnLoanVo.toString());
		try {
			RscWarnLoan rscWarnLoan = new RscWarnLoan();
			beanCopy(inRscWarnLoanVo, rscWarnLoan);
			RscWarnLoan queryRsRscWarnLoan = rscWarnLoanDao.queryByCond(rscWarnLoan);
			if (Objects.nonNull(queryRsRscWarnLoan)) {
				RscWarnLoanVO outRscWarnLoanVo = beanCopy(queryRsRscWarnLoan, new RscWarnLoanVO());
				logger.debug("当前查询结果为:"+ outRscWarnLoanVo.toString());
				return outRscWarnLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<RscWarnLoanVO> queryExAllOwner(RscWarnLoanVO rscWarnLoanVo) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryExAllOwner(rscWarnLoanVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ rscWarnLoans.size());
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public List<RscWarnLoanVO> queryExAllCurrOrg(RscWarnLoanVO rscWarnLoanVo) {
		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryExAllCurrOrg(rscWarnLoanVo);
			logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+rscWarnLoans.size());
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public List<RscWarnLoanVO> queryExAllCurrDownOrg(RscWarnLoanVO rscWarnLoanVo) {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:" + rscWarnLoanVo);
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryExAllCurrDownOrg(rscWarnLoanVo);
			logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ rscWarnLoans.size());
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public int deleteAllUserinfo(String dblNo,String custNo) {
		logger.debug("当前参数信息为:" + dblNo + custNo);
		int num = 0;
		try {
			num = rscWarnLoanDao.deleteAllUserinfo(dblNo,custNo);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return num;
	}

	@Override
	public int batchInsertRscData(List<RscWarnLoanVO> rscWarnLoanList) {
		logger.debug("当前修改数据为:"+ JSONObject.toJSONString(rscWarnLoanList));
		int num = 0;
		try {
			num = rscWarnLoanDao.batchInsertRscData(rscWarnLoanList);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ JSONObject.toJSONString(rscWarnLoanList) +"修改的数据条数为"+ num);
		return num;
	}

	@Override
	public List<RscWarnLoanVO> queryByCondByPage(RscWarnLoanVO rscWarnLoanVo) {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:" + rscWarnLoanVo);
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryByCondByPage(rscWarnLoanVo);
			logger.debug("根据客户正将号码列表查询风险预警台账的结果集数量为:"+ rscWarnLoans.size());
			pageSet(rscWarnLoans, rscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	/**
	 * 通过短信发送标识查询数据
	 *
	 * @param inRscWarnLoanVo
	 * @return
	 */
	@Override
	public List<RscWarnLoanVO> queryBySendFlagByPage(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("通过短信发送标识查询数据的参数信息为:" + inRscWarnLoanVo);
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryBySendFlagByPage(inRscWarnLoanVo);
			if(Objects.nonNull(rscWarnLoans)){
				logger.debug("根据短信发送标识查询数据的结果集数量为:"+ rscWarnLoans.size());
			}else{
				logger.debug("根据短信发送标识查询数据的结果集数量为空！");
			}
			pageSet(rscWarnLoans, inRscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	/**
	 * 通过短信发送标识查询数据统计数据量
	 *
	 * @return
	 */
	@Override
	public int queryBySendFlagCount() {
		String msg1 = "通过短信发送标识查询数据统计数据量...";
		logger.debug(msg1, "message {}");
		int num = 0;
		try {
			num = rscWarnLoanDao.queryBySendFlagCount();
		} catch (Exception e) {
			String msg2 = "通过短信发送标识查询数据统计数据量出错" + e;
			logger.debug(msg2, "message {}");
			num = -1;
		}
		return num;
	}

	/**
	 * 通过借据号更新短信发送标识
	 *
	 * @param inRscWarnLoanVo
	 * @return
	 */
	@Override
	public int updateSendFlagByBillNo(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("当前修改数据为:"+ inRscWarnLoanVo.toString());
		int num = 0;
		try {
			RscWarnLoan rscWarnLoan = new RscWarnLoan();
			beanCopy(inRscWarnLoanVo, rscWarnLoan);
			num = rscWarnLoanDao.updateSendFlagByBillNo(rscWarnLoan);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscWarnLoanVo +"修改的数据条数为"+ num);
		return num;
	}
	@Override
	public List<RscWarnLoanVO> queryByDealFlagByPage(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("通过处理标识查询数据的参数信息为:" + inRscWarnLoanVo);
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.queryByDealFlagByPage(inRscWarnLoanVo);
			if(Objects.nonNull(rscWarnLoans)){
				logger.debug("根据处理标识查询数据的结果集数量为:"+ rscWarnLoans.size());
			}else{
				logger.debug("根据处理标识查询数据的结果集数量为空！");
			}
			pageSet(rscWarnLoans, inRscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public int queryByDealFlagCount() {
		String msg1 = "通过处理标识查询数据统计数据量...";
		logger.debug(msg1, "message {}");
		int num = 0;
		try {
			num = rscWarnLoanDao.queryByDealFlagCount();
		} catch (Exception e) {
			String msg2 = "通过处理标识查询数据统计数据量出错" + e;
			logger.debug(msg2, "message {}");
			num = -1;
		}
		return num;
	}

	@Override
	public int updateDealFlagByBillNo(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("当前修改数据为:"+ inRscWarnLoanVo.toString());
		int num = 0;
		try {
			RscWarnLoan rscWarnLoan = new RscWarnLoan();
			beanCopy(inRscWarnLoanVo, rscWarnLoan);
			num = rscWarnLoanDao.updateDealFlagByBillNo(rscWarnLoan);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscWarnLoanVo +"修改的数据条数为"+ num);
		return num;
	}

	@Override
	public List<RscWarnLoanVO> queryByCertCode(String certCode, String certType) throws Exception {
		return (List<RscWarnLoanVO>) beansCopy(rscWarnLoanDao.queryByCertCode(certCode, certType), RscWarnLoanVO.class);
	}

	@Override
	public List<RscWarnLoanVO> query2BySendFlagByPage(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("通过短信发送标识查询数据的参数信息为:" + inRscWarnLoanVo);
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.query2BySendFlagByPage(inRscWarnLoanVo);
			if(Objects.nonNull(rscWarnLoans)){
				logger.debug("根据短信发送标识查询数据的结果集数量为:"+ rscWarnLoans.size());
			}else{
				logger.debug("根据短信发送标识查询数据的结果集数量为空！");
			}
			pageSet(rscWarnLoans, inRscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<RscWarnLoanVO> query2ByDealFlagByPage(RscWarnLoanVO inRscWarnLoanVo) {
		logger.debug("通过处理标识查询数据的参数信息为:" + inRscWarnLoanVo);
		List<RscWarnLoanVO> list = null;
		try {
			List<RscWarnLoan> rscWarnLoans = rscWarnLoanDao.query2ByDealFlagByPage(inRscWarnLoanVo);
			if(Objects.nonNull(rscWarnLoans)){
				logger.debug("根据处理标识查询数据的结果集数量为:"+ rscWarnLoans.size());
			}else{
				logger.debug("根据处理标识查询数据的结果集数量为空！");
			}
			pageSet(rscWarnLoans, inRscWarnLoanVo);
			list = (List<RscWarnLoanVO>) beansCopy(rscWarnLoans, RscWarnLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public int query2BySendFlagCount() {
		String msg1 = "通过短信发送标识查询数据统计数据量...";
		logger.debug(msg1, "message {}");
		int num = 0;
		try {
			num = rscWarnLoanDao.query2BySendFlagCount();
		} catch (Exception e) {
			String msg2 = "通过短信发送标识查询数据统计数据量出错" + e;
			logger.debug(msg2, "message {}");
			num = -1;
		}
		return num;
	}

	@Override
	public int query2ByDealFlagCount() {
		String msg1 = "通过处理标识查询数据统计数据量...";
		logger.debug(msg1, "message {}");
		int num = 0;
		try {
			num = rscWarnLoanDao.query2ByDealFlagCount();
		} catch (Exception e) {
			String msg2 = "通过处理标识查询数据统计数据量出错" + e;
			logger.debug(msg2, "message {}");
			num = -1;
		}
		return num;
	}
}
